package com.woniuxy.tfss.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.tfss.entity.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.woniuxy.tfss.entity.Information;
import com.woniuxy.tfss.entity.ResponseEntity;
import com.woniuxy.tfss.entity.Rider;
import com.woniuxy.tfss.entity.User;
import com.woniuxy.tfss.exception.NoAccountException;
import com.woniuxy.tfss.exception.PasswordWrongException;
import com.woniuxy.tfss.exception.ServiceException;
import com.woniuxy.tfss.mapper.*;
import com.woniuxy.tfss.service.InformationService;
import com.woniuxy.tfss.service.OrderService;
import com.woniuxy.tfss.service.UdService;
import com.woniuxy.tfss.service.UserService;
import com.woniuxy.tfss.utils.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Set;
import javax.servlet.http.HttpSession;
@Slf4j
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired
    private OrderService orderService;
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired
    private UdService udService;
    @Autowired
    private InformationService informationService;
    @Autowired(required = false)
    private UARecordMapper uaRecordMapper;
    @Autowired(required = false)
    private AddressMapper addressMapper;

    /**
     * 进入注册页面
     *
     * @param model
     * @return
     */
    @GetMapping("register")
    public String initRegister(Model model) {
        model.addAttribute("user", new User());
        return "reg";
    }

    /**
     * 跳转至用户找回密码页面
     * @param model
     * @return
     */
    @GetMapping("findPassword")
    public String findPassword(Model model) {
        model.addAttribute("user", new User());
        return "user_findPassword";
    }


    /**
     * 用户注册
     *
     * @param user         页面上传回的用户实体类，包含账号密码手机号
     * @param repassword   确认密码
     * @param putCheckcode 手机验证码
     * @return
     */
    @PostMapping("register")
    public String register(@Validated User user, String repassword, String putCheckcode, HttpSession session,Model model) {
        String phoneNum = user.getPhoneNum();
        String account = user.getAccount();
        String password = user.getPassword();
        String checkcode = (String) session.getAttribute("checkcode");
        //判断确认密码
        if (!password.equals(repassword)) {
            return "reg";
        }
        //判断短信验证码
        if (!putCheckcode.equals(checkcode)) {
            return "reg";
        }
        //根据手机号码判断是否已存在
        if (userService.getUserByPhoneNum(phoneNum) == null) {
            User regUser = new User();//regUser表示当前注册的用户
            regUser.setAccount(account);
            regUser.setPassword(password);
            regUser.setMoney(new BigDecimal("0"));
            regUser.setPhoneNum(phoneNum);
            regUser.setStatus("1");
            regUser.setRegTime(MyUtils.getNow());
            regUser.setScore(0);
            userService.save(regUser);
            Integer id = userService.getUserByPhoneNum(phoneNum).getId();
            udService.regAdd(id);
            return "userLogin";
        } else {
            //账号重复
            model.addAttribute("msg","该手机号已被注册");
            return "reg";
        }
    }

    /**
     * 用户找回密码
     * @param user
     * @param repassword
     * @param putCheckcode
     * @param session
     * @return
     */
    @PostMapping("findPassword")
    public String findPassword(@Validated User user, String repassword, String putCheckcode, HttpSession session, Model model) {
        String phoneNum = user.getPhoneNum();
        String password = user.getPassword();
        String checkcode = (String) session.getAttribute("checkcode");
        //判断确认密码
        if (!password.equals(repassword)) {
            model.addAttribute("pwdNotEq","两次输入密码不一致");
            return "user_findPassword";
        }
        //判断短信验证码
        if (!putCheckcode.equals(checkcode)) {
            model.addAttribute("checkCodeErr","验证码错误");
            return "user_findPassword";
        }
        //根据手机号码判断是否已存在
        User user1 = userService.getUserByPhoneNum(phoneNum);
        if (user1 != null) {
            userMapper.changePwdByPhoneNum(phoneNum,repassword);
            return "userLogin";
        } else {
            model.addAttribute("userNotExist","用户不存在");
            return "reg";
        }
    }

    /**
     * 跳转登录页面
     *
     * @param model
     * @return
     */
    @GetMapping("login")
    public String login(Model model) {
        model.addAttribute("user", new User());
        return "userLogin";
    }

    /**
     * 退出（回到首页）
     *
     * @param session
     * @return
     */
    @GetMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/";
    }

    /**
     * 用户登录
     *
     * @param user
     * @param model
     * @param session
     * @return
     */
    @PostMapping("login")
    public String login(User user, Model model, HttpSession session) {
        String phoneNum = user.getPhoneNum();
        String password = user.getPassword();
        User u = userService.getUserByPhoneNum(phoneNum);
        try {
            userService.login(phoneNum, password);
            session.setAttribute("user", u);
            return "redirect:/";
        } catch (ServiceException e) {
            if (e instanceof NoAccountException) {
                model.addAttribute("message1", "账号不存在或被禁用！");
                return "userLogin";
            } else if (e instanceof PasswordWrongException) {
                model.addAttribute("message2", "密码错误！");
                return "userLogin";
            } else {
                return "fail";
            }
        }
    }

    /**
     * 获取用户消费明细数据列表（在账户余额页面展示）
     * 返回值类型说明：根据layui分页需要的数据自定义返回值类型
     *
     * @param
     * @return
     */
    @ResponseBody
    @GetMapping("payList")
    public LinkedHashMap<String, Object> getPayList(HttpSession session, Integer page, Integer limit) {
        User user = (User) session.getAttribute("user");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", user.getId());
        Page<Order> pages = new Page<>(page, limit);
        IPage<Order> iPage = orderMapper.selectPage(pages, queryWrapper);
        List<Order> list = iPage.getRecords();
        long count = iPage.getTotal();
        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("code", 0);
        linkedHashMap.put("msg", "");
        linkedHashMap.put("count", count);
        linkedHashMap.put("data", list);
        return linkedHashMap;
    }

    /**
     * 查询所有订单(消费记录)--不分页用于Excel全部导出
     * @param session
     * @return
     */
    @ResponseBody
    @GetMapping("allPayList")
    public List<Order> allPayList(HttpSession session) {
        User user = (User) session.getAttribute("user");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", user.getId());
        return orderMapper.selectList(queryWrapper);
    }

    /**
     * 用户账户余额页面跳转
     *
     * @return
     */
    @GetMapping("userAccount")
    public String userAccount(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            String phoneNum = user.getPhoneNum();
            BigDecimal money = userService.getUserByPhoneNum(phoneNum).getMoney();
            model.addAttribute("money", money);
            return "member_balance";
        } else { //session过期或被清除--重新登录
            model.addAttribute("user", new User());
            return "userLogin";
        }
    }

    /**
     * 个人中心-充值页面跳转
     *
     * @return
     */
    @GetMapping("userPay")
    public String userPay(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            String phoneNum = user.getPhoneNum();
            BigDecimal money = userService.getUserByPhoneNum(phoneNum).getMoney();
            model.addAttribute("money", money);
            return "member_pay";
        } else {  //session过期或被清除--重新登录
            model.addAttribute("user", new User());
            return "userLogin";
        }
    }

    /**
     * 个人中心-充值记录页面跳转
     *
     * @return
     */
    @GetMapping("rechargeRecord")
    public String rechargeRecord(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            String phoneNum = user.getPhoneNum();
            BigDecimal money = userService.getUserByPhoneNum(phoneNum).getMoney();
            model.addAttribute("money", money);
            return "member_rechargeRecord";
        } else {  //session过期或被清除--重新登录
            model.addAttribute("user", new User());
            return "userLogin";
        }
    }

    /**
     * 获取用户充值记录数据列表（在账户充值记录页面展示）
     * 返回值类型说明：根据layui分页需要的数据自定义返回值类型
     *
     * @param session
     * @return
     */
    @ResponseBody
    @GetMapping("rechargeList")
    public LinkedHashMap<String, Object> rechargeList(HttpSession session, Integer page, Integer limit) {
        User user = (User) session.getAttribute("user");
        QueryWrapper<UserAccountRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", user.getId());
        queryWrapper.ne("type", "0");
        Page<UserAccountRecord> pages = new Page<>(page, limit);
        IPage<UserAccountRecord> iPage = uaRecordMapper.selectPage(pages, queryWrapper);
        List<UserAccountRecord> list = iPage.getRecords();
        long count = iPage.getTotal();
        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("code", 0);
        linkedHashMap.put("msg", "");
        linkedHashMap.put("count", count);
        linkedHashMap.put("data", list);
        return linkedHashMap;
    }
    /**
     * 查询所有充值--不分页用于Excel全部导出
     * @param session
     * @return
     */
    @ResponseBody
    @GetMapping("allRechargeList")
    public List<UserAccountRecord> allRechargeList(HttpSession session) {
        User user = (User) session.getAttribute("user");
        QueryWrapper<UserAccountRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", user.getId());
        queryWrapper.ne("type", "0");
        return uaRecordMapper.selectList(queryWrapper);
    }

    /**
     * 个人中心-优惠券页面跳转
     *
     * @return
     */
    @GetMapping("discount")
    public String discount(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Integer userId = user.getId();
            Integer discountTotal = userMapper.getDiscountsByUserId(userId).size();
            model.addAttribute("discountTotal", discountTotal);
            return "member_discount";
        } else {  //session过期或被清除--重新登录
            model.addAttribute("user", new User());
            return "userLogin";
        }
    }

    /**
     * 获取用户优惠券数据列表
     * 返回值类型说明：根据layui分页需要的数据自定义返回值类型
     *说明：因为使用了视图对象DiscountVO，而数据库中没有对应的表，
     * mybatis-plus不能直接使用Ipage分页，需要自定义分页逻辑，这里后端没有实现分页功能
     * @param session
     * @return
     */
    @ResponseBody
    @GetMapping("discountList")
    public LinkedHashMap<String, Object> discountList(HttpSession session, Integer page, Integer limit) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<DiscountVO> discountVoList = userMapper.getDiscountVoByUserId(userId);
        long count = discountVoList.size();
        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("code", 0);
        linkedHashMap.put("msg", "");
        linkedHashMap.put("count", count);
        linkedHashMap.put("data", discountVoList);
        return linkedHashMap;
    }

    /**
     * 个人中心--常用信息页面跳转
     *
     * @return
     */
    @GetMapping("usualInfo")
    public String usualInfo(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return "member_usualInfo";
        } else {  //session过期或被清除--重新登录
            model.addAttribute("user", new User());
            return "userLogin";
        }
    }

    /**
     * 获取用户常用信息数据列表
     * 返回值类型说明：根据layui分页需要的数据自定义返回值类型
     *
     * @param session
     * @return
     */
    @ResponseBody
    @GetMapping("usualInfoList")
    public LinkedHashMap<String, Object> usualInfoList(HttpSession session, Integer page, Integer limit) {
        User user = (User) session.getAttribute("user");
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", user.getId());
        Page<Address> pages = new Page<>(page, limit);
        IPage<Address> iPage = addressMapper.selectPage(pages, queryWrapper);
        List<Address> list = iPage.getRecords();
        long count = iPage.getTotal();
        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("code", 0);
        linkedHashMap.put("msg", "");
        linkedHashMap.put("count", count);
        linkedHashMap.put("data", list);
        return linkedHashMap;
    }

    /**
     * 根据id删除常用信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping(value = "delUsualInfo/{id}")
    public ResponseEntity delUsualInfo(@PathVariable("id") int id) {
        try {
            int i = addressMapper.deleteById(id);
            if (i != 0) {
                return ResponseEntity.SUCCESS;
            } else {
                return ResponseEntity.FAIL;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改用户常用信息
     *
     * @param address
     * @return 说明：这里修改信息使用的表单提交没有回调函数，所以直接返回刷新后的页面
     */
    @PostMapping("updateUsualInfo")
    private String updateUsualInfoById(@ModelAttribute(value = "address") Address address) {
        try {
            int i = addressMapper.updateById(address);
            if (i != 0) {
                /* return "member_usualInfo";*/
                return "redirect:/user/usualInfo";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 添加用户常用信息
     *
     * @param address
     * @return
     */
    @ResponseBody
    @PostMapping("addUsualInfo")
    private ResponseEntity addUsualInfo(@RequestBody Address address) {
        try {
            int i = addressMapper.insert(address);
            if (i != 0) {
                return ResponseEntity.SUCCESS;
            } else {
                return ResponseEntity.FAIL;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 进入修改密码页面
     *
     * @return
     */
    @GetMapping("updatepwd")
    public String initUpdate() {
//        model.addAttribute("user", new User());
        return "member_pwd";
    }

    /**
     * 修改密码
     *
     * @param session
     * @param newPassword
     * @param passwordRepeat
     * @return
     */
    @PostMapping("updatepwd")
    public String updatePwd(HttpSession session, String newPassword, String passwordRepeat,Model model) {
//判断确认密码是否一致
        if (!passwordRepeat.equals(newPassword)) {
            session.setAttribute("msg", "密码不一致");
            return "member_pwd";
        }if(passwordRepeat==""||newPassword==""){
            session.setAttribute("msg", "密码不能为空");
            return "member_pwd";
        }
        User user = (User) session.getAttribute("user");
        user.setPassword(newPassword);
        userService.updateById(user);
        session.setAttribute("msg", "修改成功");
        return "member_pwd";
    }

    /**
     * 发送短信验证码
     *
     * @param session
     * @return
     */
    @ResponseBody
    @GetMapping("checkcode/{phonenum}")
    public ResponseEntity getCheckCode(@PathVariable String phonenum, HttpSession session) {
        //产生4位验证码
        String checkCode = MyUtils.getFourRandom();
        //存入session以便注册时与输入的验证码比对
        session.setAttribute("checkcode", checkCode);
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "8a216da873cec13201746c57d1e23db9";
        String accountToken = "5ee3780bb4c548a8b84dd791bb5568c9";
        //请使用管理控制台中已创建应用的APPID
        String appId = "8a216da873cec13201746c57d2ce3dbf";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort, true);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_XML);
        // 接受验证码的手机号码
        String to = phonenum;
        if (to == null) {
            return ResponseEntity.FAIL;
        }
        //短信模板
        String templateId = "1";
        /**
         * 短信内容
         * checkCode----生成的验证码
         * "30"----有效时间
         */
        String[] datas = {checkCode, "30"};
        //短信接口
        HashMap<String, Object> result = sdk.sendTemplateSMS(to, templateId, datas);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
            return ResponseEntity.SUCCESS;
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            return ResponseEntity.FAIL;
        }
    }

    @GetMapping("getInformations/{userId}")
    @ResponseBody
    public ResponseEntity<?> getInformationsByUserId(@PathVariable Integer userId) {//获取用户的消息列表
        return new ResponseEntity<>(informationService.getByUserId(userId));
    }

    @GetMapping("initcost")
    public String initCost(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "userLogin";
        }
        return "member_cost";
    }

    @GetMapping("cost")
    @ResponseBody
    public ResponseEntity getCost(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<UserAccountRecord> uas = uaRecordMapper.getByUserId(user.getId());

        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //每月消费金额的数组
        BigDecimal[] costMoney = {new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0), new BigDecimal(0)};

        uas.forEach(u -> {
            String time = u.getTime();
            //定义每个月份的时间，一年12月
            String time1 = "2020-01-01 00:00:00";
            String time2 = "2020-02-01 00:00:00";
            String time3 = "2020-03-01 00:00:00";
            String time4 = "2020-04-01 00:00:00";
            String time5 = "2020-05-01 00:00:00";
            String time6 = "2020-06-01 00:00:00";
            String time7 = "2020-07-01 00:00:00";
            String time8 = "2020-08-01 00:00:00";
            String time9 = "2020-09-01 00:00:00";
            String time10 = "2020-10-01 00:00:00";
            String time11 = "2020-11-01 00:00:00";
            String time12 = "2020-12-01 00:00:00";
            String time13 = "2021-01-01 00:00:00";
            //将String转化为long类型
            try {
                long l = sim.parse(time).getTime();
                long l1 = sim.parse(time1).getTime();
                long l2 = sim.parse(time2).getTime();
                long l3 = sim.parse(time3).getTime();
                long l4 = sim.parse(time4).getTime();
                long l5 = sim.parse(time5).getTime();
                long l6 = sim.parse(time6).getTime();
                long l7 = sim.parse(time7).getTime();
                long l8 = sim.parse(time8).getTime();
                long l9 = sim.parse(time9).getTime();
                long l10 = sim.parse(time10).getTime();
                long l11 = sim.parse(time11).getTime();
                long l12 = sim.parse(time12).getTime();
                long l13 = sim.parse(time13).getTime();
                if (l >= l1 && l < l2) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[0] = costMoney[0].add(totalMoney);
                } else if (l >= l2 && l < l3) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[1] = costMoney[1].add(totalMoney);
                } else if (l >= l3 && l < l4) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[2] = costMoney[2].add(totalMoney);
                } else if (l >= l4 && l < l5) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[3] = costMoney[3].add(totalMoney);
                } else if (l >= l5 && l < l6) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[4] = costMoney[4].add(totalMoney);
                } else if (l >= l6 && l < l7) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[5] = costMoney[5].add(totalMoney);
                } else if (l >= l7 && l < l8) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[6] = costMoney[6].add(totalMoney);
                } else if (l >= l8 && l < l9) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[7] = costMoney[7].add(totalMoney);
                } else if (l >= l9 && l < l10) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[8] = costMoney[8].add(totalMoney);
                } else if (l >= l10 && l < l11) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[9] = costMoney[9].add(totalMoney);
                } else if (l >= l11 && l < l12) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[10] = costMoney[10].add(totalMoney);
                } else if (l >= l12 && l < l13) {
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    if (u.getType().equals("0")) {
                        totalMoney = totalMoney.add(u.getMoney());
                    } else if (u.getType().equals("2")) {
                        totalMoney = totalMoney.subtract(u.getMoney());
                    }
                    costMoney[11] = costMoney[11].add(totalMoney);
                }
            } catch (ParseException parseException) {
                parseException.printStackTrace();
            }
        });
        for (BigDecimal bigDecimal : costMoney) {
            System.out.println(bigDecimal);
        }
        return new ResponseEntity<>(costMoney);
    }
}
